package rendering;

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
import java.io.*;
import javax.imageio.*;
import riotlogic.*;
import java.awt.image.*;


public class Scenario extends JPanel implements ActionListener{
    private Terrain terrain;
    private Thread animator;
    private AntiRioter antiriot;
    Timer timer;

    public Scenario(){
        addKeyListener(new PAdapter());
        setFocusable(true);
        setDoubleBuffered(true);

        Player.makeInstance("Campillo", 100, 5, new MeleeWeapon("knife",10,1), 20, 20);
        antiriot = new AntiRioter("Mr LOL", 100, 5, new MeleeWeapon("Baton",10,1), 500, 20);


        timer = new Timer(10, this);
        timer.start();

    }

    public void paint(Graphics g)
    {
        super.paint(g);
        Graphics2D g2d = (Graphics2D)g;
        AffineTransform tx = new AffineTransform();
        tx.translate(Player.getInstance().getX(), Player.getInstance().getY()); //X,Y from the object
        //tx.rotate(Math.PI/1.4);//Object angle , ROTATE SECOND!! FOR TOP DOWN

        if(Player.getInstance().getFacing() == 'L'){
                tx.scale(-1, 1);
                tx.translate(-80, 00);
        }
        //this : on terrain. Short on time.

        AffineTransform tx2 = new AffineTransform();
        tx2.translate(antiriot.getX(), antiriot.getY());
        if(antiriot.getFacing() == 'L'){
                tx2.scale(-1, 1);
                tx2.translate(-80, 00);
        }
        g2d.drawImage(antiriot.getSprite(),tx2,this);
        g2d.drawImage(Player.getInstance().getSprite(),tx,this);
        //For every tangible object in scenario, draw them on their X,Y. Sounds simple, right? ha
        //Use timers for animation.
        Toolkit.getDefaultToolkit().sync();
        g.dispose();//The trailer
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        this.antiriot.simulatedGravityEffect();
        Player.getInstance().simulatedGravityEffect();
        repaint();
    }

    private class PAdapter extends KeyAdapter{
        public void keyPressed(KeyEvent e)
        {
            Player.getInstance().keyPressed(e);
            //repaint();
        }
    }
}
